﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Acms.Bll;
using System.Text;
using Acms.AcmsControls;
using System.Net.Mail;

public partial class UserControls_BookAppointment : BaseUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnRequest.Click += new EventHandler(btnRequest_Click);
        if (!IsPostBack)
        {

            TemplatePage acmsPage = Page as TemplatePage;
            /*ddlWith.Items.Add(new ListItem("", ""));
            if (acmsPage != null)
            {
                List<Post> services = Post.GetAllBySiteIdAndParentPath(acmsPage.AcmsContext.SiteId, "~/Services", acmsPage.AcmsContext.SiteVersion);
                List<Post> sectors = Post.GetAllBySiteIdAndParentPath(acmsPage.AcmsContext.SiteId, "~/Sectors", acmsPage.AcmsContext.SiteVersion);
                List<Post> people = Post.GetAllBySiteIdAndParentPath(acmsPage.AcmsContext.SiteId, "~/KeyPeople/FeeEarners", acmsPage.AcmsContext.SiteVersion);
                AddToWith("Services", ddlWith, services);
                AddToWith("Sectors", ddlWith, sectors);
                AddToWith("People", ddlWith, people);
            }
            txtTime.MinDate = DateTime.Today.AddDays(1);
            while (txtTime.MinDate.DayOfWeek == DayOfWeek.Saturday || txtTime.MinDate.DayOfWeek == DayOfWeek.Sunday)
            {
                txtTime.MinDate = txtTime.MinDate.AddDays(1);
            }
            
            txtTime.MaxDate = DateTime.Today.AddMonths(3);
            while (txtTime.MaxDate.DayOfWeek == DayOfWeek.Saturday || txtTime.MaxDate.DayOfWeek == DayOfWeek.Sunday)
            {
                txtTime.MaxDate = txtTime.MaxDate.AddDays(-1);
            }

            DateTime tmpSat = DateTime.Today;
            DateTime tmpSun = DateTime.Today;
            while(tmpSat.DayOfWeek != DayOfWeek.Saturday)
            {
                tmpSat = tmpSat.AddDays(1);
            }
            while(tmpSun.DayOfWeek != DayOfWeek.Sunday)
            {
                tmpSun = tmpSun.AddDays(1);
            }

            RadCalendarDay tmp = new RadCalendarDay();
            tmp.Date = tmpSat;
            tmp.IsSelectable = false;
            tmp.Repeatable = Telerik.Web.UI.Calendar.RecurringEvents.Week;

            RadCalendarDay tmp2 = new RadCalendarDay();
            tmp2.Date = tmpSun;
            tmp2.IsSelectable = false;
            tmp2.Repeatable = Telerik.Web.UI.Calendar.RecurringEvents.Week;

            txtTime.Calendar.SpecialDays.Add(tmp);
            txtTime.Calendar.SpecialDays.Add(tmp2);
            */
        }
    }

    void btnRequest_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //Send Email
            var email = Acms.Bll.Email.CreateInstance("Bevans", "info@bevans.co.uk", Server.MapPath("~/Emails/Appointment.xml"));
            email.Format("Name", txtName.Text);
            email.Format("Email", txtEmail.Text);
            email.Format("Tel", txtTel.Text);
            email.Format("Message", txtMessage.Text);
            email.Send();

            pnlApointment.Visible = false;
            pnlConfirm.Visible = true;
            lblConfimName.Text = txtName.Text;
        }
    }

    private void AddGroupNameToWith(string name, DropDownList list)
    {
        StringBuilder tmp = new StringBuilder("---");
        tmp.Append(name);
        while (tmp.Length < 14)
        {
            tmp.Append("-");
        }
        list.Items.Add(new ListItem(tmp.ToString(), ""));
    }

    private void AddToWith(string groupName, DropDownList list, string[] names)
    {
        if (names.Length > 0)
        {
            AddGroupNameToWith(groupName, list);
            foreach (string n in names)
            {
                list.Items.Add(new ListItem(n, n));
            }
            list.Items.Add(new ListItem("", ""));
        }
    }

    private void AddToWith(string groupName, DropDownList list, List<Topic> topics)
    {
        string[] tmp = new string[topics.Count];
        for (int i = 0; i < topics.Count; i++)
        {
            tmp[i] = topics[i].Name;
        }
        AddToWith(groupName, list, tmp);
    }

    private void AddToWith(string groupName, DropDownList list, List<Post> posts)
    {
        string[] tmp = new string[posts.Count];
        for (int i = 0; i < posts.Count; i++)
        {
            tmp[i] = posts[i].DisplayName;
        }
        AddToWith(groupName, list, tmp);
    }
}
